home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / fax.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  183 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    fax -
  19.  *        Support for writing fax files
  20.  *
  21.  *                Paul Haeberli - 1989
  22.  */
  23. #include "stdio.h"
  24. #include "fax.h"
  25.  
  26. static puteol();
  27. static flushbits();
  28. static putwhitespan();
  29. static putblackspan();
  30. static putcode();
  31. static int putbit();
  32.  
  33. /* 
  34.  *    generic fax stuff follows 
  35.  *
  36.  */
  37. static FILE *faxf;
  38. static int shdata;
  39. static int shbit = 0x01;
  40.  
  41. openfax(name)
  42. char *name;
  43. {
  44.  
  45.     faxf = fopen(name,"w");
  46.     if(!faxf) {
  47.     fprintf(stderr,"openfax: can't open output file %s\n",name);
  48.     exit(1);
  49.     }
  50. }
  51.  
  52. closefax()
  53. {
  54.     int i;
  55.  
  56.     for(i=0; i<10; i++)
  57.     puteol();
  58.     flushbits(faxf);
  59.     fclose(faxf);
  60. }
  61.  
  62. tofax(sbuf,n)
  63. short *sbuf;
  64. int n;
  65. {
  66.     int c = 0;
  67.  
  68.     puteol();
  69.     while(n>0) { 
  70.     c = 0;
  71.     while(*sbuf>128 && n>0) {
  72.         sbuf++;
  73.         c++;
  74.         n--;
  75.     }
  76.     putwhitespan(c);
  77.     c = 0;
  78.     if(n==0)
  79.         break;
  80.     while(*sbuf<=128 && n>0) {
  81.         sbuf++;
  82.         c++;
  83.         n--;
  84.     }
  85.     if(n>0 && c>0)
  86.         putblackspan(c);
  87.     }
  88. }
  89.  
  90. static putwhitespan(c)
  91. int c;
  92. {
  93.     int tpos;
  94.     tableentry *te;
  95.  
  96.     if(c>=64) {
  97.     tpos = (c/64)-1;
  98.     te = mwtable+tpos;
  99.     c -= te->count;
  100.     putcode(te);
  101.     }
  102.     tpos = c;
  103.     te = twtable+tpos;
  104.     putcode(te);
  105. }
  106.  
  107. static putblackspan(c)
  108. int c;
  109. {
  110.     int tpos;
  111.     tableentry *te;
  112.  
  113.     if(c>=64) {
  114.     tpos = (c/64)-1;
  115.     te = mbtable+tpos;
  116.     c -= te->count;
  117.     putcode(te);
  118.     }
  119.     tpos = c;
  120.     te = tbtable+tpos;
  121.     putcode(te);
  122. }
  123.  
  124. static putcode(te)
  125. tableentry *te;
  126. {
  127.     unsigned int mask;
  128.     int code;
  129.  
  130.     mask = 1<<(te->length-1);
  131.     code = te->code;
  132.     while(mask) {
  133.      if(code&mask)
  134.         putbit(1);
  135.     else
  136.         putbit(0);
  137.     mask >>= 1;
  138.     }
  139.  
  140. }
  141.  
  142. static puteol()
  143. {
  144.     int i;
  145.  
  146.     for(i=0; i<11; i++)
  147.     putbit(0);
  148.     putbit(1);
  149. }
  150.  
  151. static putzeros()
  152. {
  153.     int i;
  154.  
  155.     for(i=0; i<64; i++)
  156.     putbit(0);
  157. }
  158.  
  159. static int putbit(d)
  160. int d;
  161. {
  162.     int i;
  163.     int b;
  164.  
  165.     if(d) 
  166.     shdata = shdata|shbit;
  167.     shbit = shbit<<1;
  168.     if((shbit&0xff) ==0) {
  169.     putc(shdata,faxf);
  170.     shdata = 0;
  171.     shbit = 0x01;
  172.     }
  173. }
  174.  
  175. static flushbits()
  176. {
  177.     if(shbit!=0x80) {
  178.     putc(shdata,faxf);
  179.     shdata = 0;
  180.     shbit = 0x01;
  181.     }
  182. }
  183.